package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ItemActivity;
import flc.ast.activity.WeaponActivity;
import flc.ast.adapter.ItemAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.adapter.TopicAdapter;
import flc.ast.adapter.WeaponAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private RecommendAdapter recommendAdapter;
    private TopicAdapter topicAdapter;
    private WeaponAdapter weaponAdapter;
    private ItemAdapter itemAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivWeaponMore.setOnClickListener(this);
        mDataBinding.ivItemMore.setOnClickListener(this);
        //每日推荐
        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);
        //热门话题
        mDataBinding.rvTopicList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        topicAdapter = new TopicAdapter();
        mDataBinding.rvTopicList.setAdapter(topicAdapter);
        topicAdapter.setOnItemClickListener(this);
        //武器库
        mDataBinding.rvWeaponList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        weaponAdapter = new WeaponAdapter();
        mDataBinding.rvWeaponList.setAdapter(weaponAdapter);
        weaponAdapter.setOnItemClickListener(this);
        //物品库
        mDataBinding.rvItemList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        itemAdapter = new ItemAdapter();
        mDataBinding.rvItemList.setAdapter(itemAdapter);
        itemAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivWeaponMore://武器库
                startActivity(WeaponActivity.class);
                break;
            case R.id.ivItemMore://物品库
                startActivity(ItemActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getBannerData();
        getRecommendData();
        getTopicData();
        getWeaponData();
        getItemData();
    }

    private void getBannerData() {
        StkResApi.getTagResourceList(this, AppConfig.BANNER_DATA, StkApi.createParamMap(0, 5), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                MyBannerAdapter bannerAdapter = new MyBannerAdapter(result, getContext());
                                mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
                                mDataBinding.homeBanner.isAutoLoop(true);//false不自动轮播
                                //点击监听
                                mDataBinding.homeBanner.setOnBannerListener(new OnBannerListener() {
                                    @Override
                                    public void OnBannerClick(Object data, int position) {
                                        StkResBean bean = (StkResBean) bannerAdapter.getData(position);
                                        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
                                    }
                                });
                            }
                        }
                    }
                });
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getTopicData() {
        StkResApi.getTagResourceList(this, AppConfig.TOPIC_DATA, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        topicAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getWeaponData() {
        StkResApi.getTagResourceList(this, AppConfig.WEAPON_DATA1, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        weaponAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getItemData() {
        StkResApi.getTagResourceList(this, AppConfig.ITEM_DATA3, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        itemAdapter.setList(result);
                    }
                }
            }
        });
    }

}